﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using KegData;
using KegCommunication;

namespace KegMonitor
{
    public static class ConsoleEx
    {
        /// <summary>
        /// Shows the Console
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
        /// <summary>
        /// Hides the Console
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
    } 

    public static class Program
    {
        private static Thread KegMonitorThread;
        private static KegReader kreader;
        private static Form1 MainWindow;

        public static KegReader KegBot
        {
            get { return kreader; }
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                string port = Properties.Settings.Default.Port;
                int taps = Properties.Settings.Default.Taps;

                KegWriter.Initialize(Helper.ConnectionString);
                kreader = new KegReader(port, taps);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                MainWindow = new Form1();

                StartKegMonitor();

                Application.Run(MainWindow);

                // TODO: This is like using a sledgehammer to kill a spider, 
                if (KegMonitorThread != null && KegMonitorThread.ThreadState == ThreadState.Running)
                {
                    KegMonitorThread.Abort();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Big Failure");
            }
        }

        public static void StartKegMonitor()
        {
            if (KegMonitorThread != null)
            {
                StopKegMonitor();
            }

            if (KegMonitorThread == null)
            {
                KegLog.LogComment("Starting the kegmonitor");
                KegMonitorThread = new Thread(new ThreadStart(KegMonitorThreadStart));
                KegMonitorThread.Start();
            }
        }

        public static void StopKegMonitor()
        {
            // Kill the kegmonitor thread
            if (KegMonitorThread != null)
            {
                kreader.StopReading();
                KegMonitorThread.Abort();
                KegMonitorThread = null;
                kreader.Status = KegBotStatus.Stopped;
            }
        }

        static void KegMonitorThreadStart()
        {
            kreader.Port = Properties.Settings.Default.Port;
            kreader.Taps = Properties.Settings.Default.Taps;

            if (kreader.StartReading())
            {
                // Verify that this is a KegBot
                kreader.Ping();
                while (true)
                {
                    // TODO: Main app needs to tell this thread to exit gracefully.
                }
            }
        }
    }
}
